package com.sixth.service.impl;

import com.sixth.bean.vo.GoodsStatisticsVO;
import com.sixth.bean.vo.OrderStatisticsVO;
import com.sixth.bean.vo.StatisticsVO;
import com.sixth.bean.vo.UserUpVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketGoodsMapper;
import com.sixth.mapper.MarketOrderMapper;
import com.sixth.mapper.MarketUserMapper;
import com.sixth.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Chen
 * @Date 2022-09-09  14-31-43
 * @description 统计平台数据
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {


    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketGoodsMapper  goodsMapper;
    /**
     * @Author Chen
     * @Date 2022-09-09  14-31-58
     * @param
     * @return {@link null }
     * @description  按日统计用户增长数
     */
    @Override
    public StatisticsVO getUserUpNum() {


        List<UserUpVO> userUpVOS = userMapper.countUserByDay();

        if (userUpVOS!=null&&userUpVOS.size()>= Constant.ONE){

            StatisticsVO<UserUpVO> userStatisticsVO = new StatisticsVO<UserUpVO>();

            ArrayList<String> columns = new ArrayList<String>();
            columns.add("day");
            columns.add("users");

            userStatisticsVO.setColumns(columns);

            userStatisticsVO.setRows(userUpVOS);

            return userStatisticsVO;
        }

        return null;
    }

    /**
     * @Author Chen
     * @Date 2022-09-09  16-42-40
     * @param
     * @return {@link null }
     * @description 统计订单数据
     */
    @Override
    public StatisticsVO getOrderStatistics() {

        List<OrderStatisticsVO> orderStatisticsVOS = orderMapper.countOrdersByDay();

        if (orderStatisticsVOS!=null&&orderStatisticsVOS.size()>= Constant.ONE){

            for (OrderStatisticsVO orderStatisticsVO : orderStatisticsVOS) {
                orderStatisticsVO.setPcr(orderStatisticsVO.getAmount()/orderStatisticsVO.getCustomers());
            }

            StatisticsVO<OrderStatisticsVO> orderStatisticsVO = new StatisticsVO<OrderStatisticsVO>();

            ArrayList<String> columns = new ArrayList<String>();
            columns.add("day");
            columns.add("orders");
            columns.add("customers");
            columns.add("amount");
            columns.add("pcr");

            orderStatisticsVO.setColumns(columns);

            orderStatisticsVO.setRows(orderStatisticsVOS);

            return orderStatisticsVO;
        }
        return null;
    }


    //按日统计商品订单量
    @Override
    public StatisticsVO getGoodsStatistics() {

        List<GoodsStatisticsVO> goodsStatisticsVOS = goodsMapper.countGoodsByDay();

        if (goodsStatisticsVOS!=null && goodsStatisticsVOS.size() >= Constant.ONE){

            StatisticsVO<GoodsStatisticsVO> goodsStatisticsVO = new StatisticsVO<GoodsStatisticsVO>();
            //建一个list去封装columns
            List<String> columns = new ArrayList<>();
            //columns的四个属性，直接添加
            columns.add("day");
            columns.add("orders");
            columns.add("products");
            columns.add("amount");
            goodsStatisticsVO.setColumns(columns);//直接将封装好的columns赋值

            goodsStatisticsVO.setRows(goodsStatisticsVOS);

            return goodsStatisticsVO;
        }
        return null;
    }


}
